﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace auxiliary_proj
{
    public partial class FormNewMessage : Form
    {
        FormComposeMessage fcm;
        FormShowMessage fsm;

        static Font f = new Font("Arial", 8, FontStyle.Regular); //Set the default font
        private static DataTable table;
        private static MySqlConnection DBCon;

        public FormNewMessage()
        {
            InitializeComponent();

            //the double click handler
            this.messageInbox.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.messageInbox_CellContentDoubleClick);

            //paint handler for dataGridView
            this.messageInbox.Paint += new System.Windows.Forms.PaintEventHandler(this.messageInbox_Paint);

            Program.parseServerInfo();
            DBCon = new MySqlConnection(Program.getConnection()); //get connection 
            qReceiveMsg(Employee.User);
        }

        // Update the message to viewed if the user clicks on it
        private void updateViewed(int msg)
        {
            string query = "update MESSAGING set isViewed=" + 1 + " where message_id='" + msg + "'";
            Query.nonQuery(query);
        }

        // Queries the database to get all the messages for the user that is logged in
        private void qReceiveMsg(string receive_emp)
        {
            string query = "SELECT CONCAT( u.first_name,  ' ', u.last_name ) AS 'From', m.subject AS 'Subject', m.timestamp AS 'Date', m.message AS 'message', m.send_user_id AS 'Sender', m.isViewed AS 'Viewed', m.message_id AS 'ID'" +
                "FROM MESSAGING AS m, USER AS u WHERE m.receive_user_id = \"" + receive_emp + "\" AND u.user_name = m.send_user_id ORDER BY TIMESTAMP DESC ";
            table = Query.select(query);
            messageInbox.DataSource = table;
            updateView();
        }

        // Queries the database to get all the unread messages for the user that is logged in
        private void qUnreadReceiveMsg(string receive_emp)
        {
            string query = "SELECT CONCAT( u.first_name,  ' ', u.last_name ) AS 'From', m.subject AS 'Subject', m.timestamp AS 'Date', m.message AS 'message', m.send_user_id AS 'Sender', m.isViewed AS 'Viewed', m.message_id AS 'ID'" +
                            "FROM MESSAGING AS m, USER AS u WHERE m.receive_user_id = \"" + receive_emp + "\" AND m.isViewed =" + 0 + " AND u.user_name = m.send_user_id ORDER BY TIMESTAMP DESC ";
            table = Query.select(query);
            messageInbox.DataSource = table;
        }

        private void updateView()
        {
            for (int i = 0; i < messageInbox.Rows.Count; i++)
            {
                if ((int)table.Rows[i]["Viewed"] == 0) //Sets the fontstyle to bold if the message hasnt been read
                {
                    this.messageInbox.Rows[i].DefaultCellStyle.Font = new Font(f.FontFamily, f.Size, FontStyle.Bold);
                }
                else if ((int)table.Rows[i]["Viewed"] == 1) //Sets the fontstyle to regular if the message as been read
                {
                    this.messageInbox.Rows[i].DefaultCellStyle.Font = new Font(f.FontFamily, f.Size, FontStyle.Regular);
                }

            }
        }

        private void messageInbox_Paint(object sender, PaintEventArgs e)
        {
            updateView();
        }

        //creates a new form to send a message to other employees
        private void newMsgBtn_Click(object sender, EventArgs e)
        {
            fcm = new FormComposeMessage();
            fcm.StartPosition = FormStartPosition.CenterScreen;
            this.Dispose();
            fcm.Show();
        }

        // Double clicking a particular cell in the inbox will create a new form to show the message and update the viewed column of the message
        private void messageInbox_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            fsm = new FormShowMessage((string)messageInbox.Rows[e.RowIndex].Cells["From"].Value, (string)messageInbox.Rows[e.RowIndex].Cells["Subject"].Value, messageInbox.Rows[e.RowIndex].Cells["Date"].Value.ToString(), (string)messageInbox.Rows[e.RowIndex].Cells["message"].Value, messageInbox.Rows[e.RowIndex].Cells["Sender"].Value.ToString());
            fsm.Show();
            if ((int)messageInbox.Rows[e.RowIndex].Cells["Viewed"].Value == 0)
            {
                updateViewed((int)messageInbox.Rows[e.RowIndex].Cells["ID"].Value);
                qReceiveMsg(Employee.User);
                Singleton.getHome().qNewMsgCount();              
            }
        }

        private void linkLabelAllMessages_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            qReceiveMsg(Employee.User); //Displays all messages 
        }

        private void linkLabelUnreadMessages_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            qUnreadReceiveMsg(Employee.User); //Display unread messages
        }
    }
}
